package com.boc.ewr.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boc.ewr.bcimp.entity.CusmTagList;
import com.boc.ewr.business.entity.AcctDtlClassD;
import com.boc.ewr.business.entity.AcctDtlClassD;
import com.boc.ewr.business.entity.vo.AcctDtlClassDVo;
import com.boc.ewr.business.service.IAcctDtlClassDService;
import com.boc.ewr.business.service.ICusmTagRelationService;
import com.boc.ewr.util.file.enumeration.FdbkStatusEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: D类账户明细
 * @Author: jeecg-boot
 * @Date: 2023-05-26
 * @Version: V1.0
 */
@Api(tags = "D类账户明细")
@RestController
@RequestMapping("/business/acctDtlClassD")
@Slf4j
public class AcctDtlClassDController extends JeecgController<AcctDtlClassD, IAcctDtlClassDService> {
    @Autowired
    private IAcctDtlClassDService acctDtlClassDService;
    @Autowired
    private ICusmTagRelationService cusmTagRelationService;


    /**
     * 分页列表查询
     *
     * @param acctDtlClassD
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "D类账户明细-分页列表查询")
    @ApiOperation(value = "D类账户明细-分页列表查询", notes = "D类账户明细-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<AcctDtlClassDVo>> queryPageList(AcctDtlClassD acctDtlClassD,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                        HttpServletRequest req) {
        QueryWrapper<AcctDtlClassD> queryWrapper = QueryGenerator.initQueryWrapper(acctDtlClassD, req.getParameterMap());
        Page<AcctDtlClassD> page = new Page<AcctDtlClassD>(pageNo, pageSize);
        IPage<AcctDtlClassD> pageList = acctDtlClassDService.page(page, queryWrapper);
        IPage<AcctDtlClassDVo> convertIPage = this.convertBusInfoPage(pageList);
        return Result.OK(convertIPage);
    }

    /**
     * 添加
     *
     * @param acctDtlClassD
     * @return
     */
    @AutoLog(value = "D类账户明细-添加")
    @ApiOperation(value = "D类账户明细-添加", notes = "D类账户明细-添加")
    //@RequiresPermissions("com.boc.ewr:acct_dtl_class_d:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody AcctDtlClassD acctDtlClassD) {
        acctDtlClassDService.save(acctDtlClassD);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param acctDtlClassD
     * @return
     */
    @AutoLog(value = "D类账户明细-编辑")
    @ApiOperation(value = "D类账户明细-编辑", notes = "D类账户明细-编辑")
    //@RequiresPermissions("com.boc.ewr:acct_dtl_class_d:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody AcctDtlClassD acctDtlClassD) {
        acctDtlClassDService.updateById(acctDtlClassD);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "D类账户明细-通过id删除")
    @ApiOperation(value = "D类账户明细-通过id删除", notes = "D类账户明细-通过id删除")
    //@RequiresPermissions("com.boc.ewr:acct_dtl_class_d:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        acctDtlClassDService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "D类账户明细-批量删除")
    @ApiOperation(value = "D类账户明细-批量删除", notes = "D类账户明细-批量删除")
    //@RequiresPermissions("com.boc.ewr:acct_dtl_class_d:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.acctDtlClassDService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "D类账户明细-通过id查询")
    @ApiOperation(value = "D类账户明细-通过id查询", notes = "D类账户明细-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<AcctDtlClassD> queryById(@RequestParam(name = "id", required = true) String id) {
        AcctDtlClassD acctDtlClassD = acctDtlClassDService.getById(id);
        if (acctDtlClassD == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(acctDtlClassD);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param acctDtlClassD
     */
    //@RequiresPermissions("com.boc.ewr:acct_dtl_class_d:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AcctDtlClassD acctDtlClassD) {
        return super.exportXls(request, acctDtlClassD, AcctDtlClassD.class, "D类账户明细");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("acct_dtl_class_d:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AcctDtlClassD.class);
    }

    /**
     * 反馈，后台自动赋值反馈人，反馈时间
     *
     * @param acctDtlClassD
     * @return
     */
    @AutoLog(value = "D类账户明细-反馈")
    @ApiOperation(value = "D类账户明细-反馈", notes = "D类账户明细-反馈")
    //@RequiresPermissions("com.boc.ewr:emp_brk_fdbk:edit")
    @RequestMapping(value = "/fdbk", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> fdbk(@RequestBody AcctDtlClassD acctDtlClassD) {
        AcctDtlClassD acctDtl = acctDtlClassDService.getById(acctDtlClassD.getId());
        //反馈信息更新
        acctDtl.setAppendFile(acctDtlClassD.getAppendFile());
        acctDtl.setIsCa(acctDtlClassD.getIsCa());
        acctDtl.setOtherReason(acctDtlClassD.getOtherReason());
        acctDtl.setStatus(acctDtlClassD.getStatus());
        acctDtl.setUnfinReason(acctDtlClassD.getUnfinReason());
        //获取反馈人和反馈时间
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        acctDtl.setRiskCheckPsn(user.getRealname());
        acctDtl.setRiskCheckMob(user.getPhone());
        acctDtl.setRiskCheckTime(new Date());
        // 设置反馈状态为未审核
        acctDtl.setFdbkStatus(FdbkStatusEnum.NOAPPROVE.getIndex());

        acctDtlClassDService.updateById(acctDtl);
        return Result.OK("反馈成功!");
    }

    /**
     * 审核退回
     *
     * @param acctDtlClassD
     * @return
     */
    @AutoLog(value = "D类账户明细-审核退回")
    @ApiOperation(value = "D类账户明细-审核退回", notes = "D类账户明细-审核退回")
    @RequestMapping(value = "/reject", method = {RequestMethod.PUT, RequestMethod.POST})
    //@RequiresPermissions("com.boc.ewr:ca_cusm_call_olfdbk:reject")
    public Result<String> reject(@RequestBody AcctDtlClassD acctDtlClassD) {
        AcctDtlClassD acctDtl = acctDtlClassDService.getById(acctDtlClassD.getId());
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        acctDtl.setCheckPsn(user.getRealname());
        acctDtl.setCheckTime(new Date());
        // 设置反馈状态为退回
        acctDtl.setFdbkStatus(FdbkStatusEnum.REJECT.getIndex());

        acctDtlClassDService.updateById(acctDtl);
        return Result.OK("退回成功!");
    }

    /**
     * 审核通过
     *
     * @param acctDtlClassD
     * @return
     */
    @AutoLog(value = "C类账户明细-审核通过")
    @ApiOperation(value = "C类账户明细-审核通过", notes = "C类账户明细-审核通过")
    @RequestMapping(value = "/approve", method = {RequestMethod.PUT, RequestMethod.POST})
    //@RequiresPermissions("com.boc.ewr:ca_cusm_call_olfdbk:approve")
    public Result<String> approve(@RequestBody AcctDtlClassD acctDtlClassD) {
        AcctDtlClassD acctDtl = acctDtlClassDService.getById(acctDtlClassD.getId());

        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        acctDtl.setCheckPsn(user.getRealname());
        acctDtl.setCheckTime(new Date());
        // 审核通过才设置为反馈成功（审核通过）
        acctDtl.setFdbkStatus(FdbkStatusEnum.FEEDBACK.getIndex());
        // 判断反馈是否逾期
        //TODO

        //acctDtl.setIsDelinquent(this.isDelinquent(caCusmCallOlfdbkById));


        acctDtlClassDService.updateById(acctDtl);
        return Result.OK("审核通过!");
    }

    //@AutoLog(value = "D类账户明细-通过cusmNm查询标签")
    @ApiOperation(value = "D类账户明细-通过cusmNm查询标签", notes = "D类账户明细-通过cusmNm查询标签")
    @GetMapping(value = "/queryTageByCusmNm")
    public Result<Map<String, List<CusmTagList>>> queryTageByCusmNm(@RequestParam(name = "cusmNm", required = true) String cusmNm) {

        Map<String, List<CusmTagList>> map = cusmTagRelationService.queryTageByCusmNm(cusmNm);
        if (map.size() == 0) {
            return Result.error("未根据客户名称查询到客户信息！");
        } else {
            return Result.OK(map);
        }
    }

    //@AutoLog(value = "D类账户明细-通过CusmNm刷新标签")
    @ApiOperation(value = "D类账户明细-通过CusmNm刷新标签", notes = "D类账户明细-通过CusmNm刷新标签")
    @GetMapping(value = "/updateTagByCusmNm")
    public Result<String> updateTagByCusmNm(@RequestParam(name = "cusmNm", required = true) String cusmNm) {

        cusmTagRelationService.updateTagCusmNm(cusmNm);

        return Result.OK("标签刷新成功");
    }

    /**
     * 将 Page转出成Vo的Page
     */
    public IPage<AcctDtlClassDVo> convertBusInfoPage(IPage<AcctDtlClassD> page) {
        List<AcctDtlClassDVo> voList = page.getRecords().stream()
                .map(this::convertAcctDtl)
                .collect(Collectors.toList());

        IPage<AcctDtlClassDVo> voPage = new Page<>();
        voPage.setCurrent(page.getCurrent());
        voPage.setSize(page.getSize());
        voPage.setTotal(page.getTotal());
        voPage.setRecords(voList);

        return voPage;
    }

    private AcctDtlClassDVo convertAcctDtl(AcctDtlClassD entity) {
        AcctDtlClassDVo vo = new AcctDtlClassDVo();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }
}
